package com.unnamed.b.atv.sample.ability;

import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.sample.ResourceTable;
import com.unnamed.b.atv.sample.holder.IconTreeItemHolder;
import com.unnamed.b.atv.view.OhosTreeView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.PopupDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;

/**
 * FolderStructureAbility
 *
 * @author Administrator
 * @Date 2021/5/26
 */
public class FolderStructureAbility extends Ability {
    private Text statusBar;
    private OhosTreeView tView;
    private ToastDialog toastDialog;
    private PopupDialog popupDialog;
    private Image more;
    private Component dialogView;
    private int counter = 0;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        setUIContent(ResourceTable.Layout_layout_folder);
        initView();
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    private void initView() {
        more = (Image) findComponentById(ResourceTable.Id_more);
        ComponentContainer componentContainer = (ComponentContainer) findComponentById(ResourceTable.Id_container);
        statusBar = (Text) findComponentById(ResourceTable.Id_status_bar);
        TreeNode root = TreeNode.root();
        TreeNode computerRoot = new TreeNode(new IconTreeItemHolder.IconTreeItem
            (ResourceTable.Media_compter, "My Computer"));
        TreeNode myDocuments = new TreeNode(new IconTreeItemHolder.IconTreeItem
            (ResourceTable.Media_folder_close, "My Documents"));
        TreeNode downloads = new TreeNode(new IconTreeItemHolder.IconTreeItem
            (ResourceTable.Media_folder_close, "Downloads"));
        TreeNode file1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_file, "Folder 1"));
        TreeNode file2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_file, "Folder 2"));
        TreeNode file3 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_file, "Folder 3"));
        TreeNode file4 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_file, "Folder 4"));
        fillDownloadsFolder(downloads);
        downloads.addChildren(file1, file2, file3, file4);
        TreeNode myMedia = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_picture, "Photos"));
        TreeNode photo1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_picture, "Folder 1"));
        TreeNode photo2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_picture, "Folder 2"));
        TreeNode photo3 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_picture, "Folder 3"));
        myMedia.addChildren(photo1, photo2, photo3);
        myDocuments.addChild(downloads);
        computerRoot.addChildren(myDocuments, myMedia);
        root.addChildren(computerRoot);
        tView = new OhosTreeView(this, root);
        tView.setDefaultAnimation(true);
        tView.setDefaultViewHolder(IconTreeItemHolder.class);
        tView.setDefaultNodeClickListener(nodeClickListener);
        componentContainer.addComponent(tView.getView());
        more.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                setShowDialog();
            }
        });
    }

    private void fillDownloadsFolder(TreeNode node) {
        TreeNode downloads = new TreeNode(new IconTreeItemHolder.IconTreeItem
                (ResourceTable.Media_folder_close, "Downloads" + (counter++)));
        node.addChild(downloads);
        if (counter < 5) {
            fillDownloadsFolder(downloads);
        }
    }

    private TreeNode.TreeNodeClickListener nodeClickListener = new TreeNode.TreeNodeClickListener() {
        @Override
        public void onClick(TreeNode node, Object value) {
            IconTreeItemHolder.IconTreeItem item = (IconTreeItemHolder.IconTreeItem) value;
            statusBar.setText("Last clicked: " + item.getText());
        }
    };

    private TreeNode.TreeNodeLongClickListener nodeLongClickListener = new TreeNode.TreeNodeLongClickListener() {
        @Override
        public boolean onLongClick(TreeNode node, Object value) {
            IconTreeItemHolder.IconTreeItem item = (IconTreeItemHolder.IconTreeItem) value;
            toastDialog = new ToastDialog(FolderStructureAbility.this);
            toastDialog.setText("Long click: " + item.getText()).setAlignment(LayoutAlignment.BOTTOM).show();
            return true;
        }
    };

    /**
     * 初始化设置显示dialog
     */
    private void setShowDialog() {
        dialogView = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_dialog_select, null, false);
        dialogView.findComponentById(ResourceTable.Id_ExpandAll).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                setRemoveDialog();
                tView.expandAll();
            }
        });
        dialogView.findComponentById(ResourceTable.Id_CollapseAll).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                setRemoveDialog();
                tView.collapseAll();
            }
        });
        popupDialog = new PopupDialog(getContext(), more);
        popupDialog.setAlignment(LayoutAlignment.LEFT | LayoutAlignment.TOP);
        popupDialog.setCustomComponent(dialogView);
        popupDialog.setDialogListener(new BaseDialog.DialogListener() {
            @Override
            public boolean isTouchOutside() {
                setRemoveDialog();
                return false;
            }
        });
        popupDialog.show();
    }

    /**
     * 销毁dialog
     */
    private void setRemoveDialog() {
        dialogView = null;
        popupDialog.remove();
        popupDialog.destroy();
    }

}
